<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class ManageCompany extends CI_Controller{
	function __construct(){
		parent::__construct();
		$this->headerdata = array();
		if (($this->session->userdata('adminid')>0) && ($this->session->userdata('adminhash')==md5($this->session->userdata('adminid').$this->session->userdata('adminemail')))){
			$this->headerdata['adminid'] = $this->session->userdata('adminid');
			$this->headerdata['adminemail'] = $this->session->userdata('adminemail');
			$this->headerdata['username'] = $this->session->userdata('adminemail');
		}else{
			redirect(base_url('index.php/admin/login'));
		}
		
		$this->headerdata['route'] = 'AdminCP';
		$this->headerdata['routelink'] = 'admin';
		$this->headerdata['route2'] = 'Manage Companies';
		$this->headerdata['route2link'] = '/index.php/admin/managecompany';
		
		if (($this->session->userdata('msg') != '') && ($this->session->userdata('msgtype') != '')){
			$this->headerdata['msg'] = $this->session->userdata('msg');
			$this->headerdata['msgtype'] = $this->session->userdata('msgtype');
			$this->session->set_userdata('msg','');
			$this->session->set_userdata('msgtype','');
		}
		
		$this->load->model('CompanyModel');
	}
	
	function index(){
		$data = array();
		$data['list'] = $this->CompanyModel->get();
		
		$this->load->view('admin/common/header',$this->headerdata);
		$this->load->view('admin/common/left');
		$this->load->view('admin/company',$data);
		$this->load->view('admin/common/footer');
	}
	
	function add(){
		$data = array(
			'name'			=> '',
			'description'	=> '',
			'representname'	=> '',
			'phone'			=> '',
			'email'			=> '',
			'address'		=> '',
			'requirement'	=> '',
			'traineeNum'	=> ''
		);
		
		if ($this->input->post('name') != ''){
			$newdata = array(
				'name'			=> $this->input->post('name'),
				'description'	=> $this->input->post('description'),
				'email'			=> $this->input->post('representname')."|".$this->input->post('email')."|".$this->input->post('phone'),
				'address'		=> $this->input->post('address'),
				'requirement'	=> $this->input->post('requirement'),
				'traineeNum'	=> $this->input->post('traineeNum')
			);
			$data = $newdata;
			$data['phone'] = $this->input->post('phone');
			$data['representname'] = $this->input->post('representname');
			
			if (($newdata['description']=='') || ($newdata['email']=='') ||
				($newdata['traineeNum']=='') || ($newdata['address']=='') || 
				($newdata['requirement']=='')){
				
				$this->headerdata['msgtype'] = 'error';
				$this->headerdata['msg'] = 'One or more required field is missing.';
			}else{
				if ($this->CompanyModel->add($newdata)){
					$this->session->set_userdata('msg','Company added.');
					$this->session->set_userdata('msgtype','success');
					redirect(base_url($this->headerdata['route2link']));
				}else{
					$this->headerdata['msgtype'] = 'error';
					$this->headerdata['msg'] = 'Add company function is having some errors. Please try again.';
				}
			}
		}
		
		$this->headerdata['route3'] = 'Add new company';
		
		$this->load->view('admin/common/header',$this->headerdata);
		$this->load->view('admin/common/left');
		$this->load->view('admin/company_add',$data);
		$this->load->view('admin/common/footer');
	}
	
	function delete(){
		$ids = $this->input->post('selected');
		$num = $this->CompanyModel->delete($ids);
		$this->session->set_userdata('msg',$num.' companies deleted.');
		$this->session->set_userdata('msgtype','success');
		redirect(base_url($this->headerdata['route2link']));
	}
	
	function edit($id){
		$data = array();
		$company = $this->CompanyModel->get($id);
		$data = $company;
		$tmp = explode('|',$data->email);
		$data->email = $tmp[1];
		$data->representname = $tmp[0];
		$data->phone = $tmp[2];
		if ($this->input->post('name') != ''){
			$newdata = array(
				'name'			=> $this->input->post('name'),
				'description'	=> $this->input->post('description'),
				'requirement'	=> $this->input->post('requirement'),
				'email'			=> $this->input->post('representname')."|".$this->input->post('email')."|".$this->input->post('phone'),
				'address'		=> $this->input->post('address'),
				'traineeNum'	=> $this->input->post('traineeNum')
			);
			$data = $newdata;
			$data['representname'] = $this->input->post('representname');
			$data['phone'] = $this->input->post('phone');
			
			if ($this->CompanyModel->edit($id,$newdata)){
				$this->session->set_userdata('msg','Company edited.');
				$this->session->set_userdata('msgtype','success');
				redirect(base_url($this->headerdata['route2link']));
			}else{
				$this->headerdata['msgtype'] = 'error';
				$this->headerdata['msg'] = 'Edit company function is having some errors. Please try again.';
			}
		}
		$this->headerdata['route3'] = 'Edit company';
		
		
		$this->load->view('admin/common/header',$this->headerdata);
		$this->load->view('admin/common/left');
		$this->load->view('admin/company_edit',$data);
		$this->load->view('admin/common/footer');
	}
	
	function signupList($id=0){
		$this->headerdata['route2'] = 'Sign-up list';
		$data = array();
		
		if (!is_numeric($id) || ($id<-1)) $id = 0;
		
		if ($this->input->post('selected')){
			$ids = $this->input->post('selected');
			foreach ($ids as $delid){
				$this->CompanyModel->deleteSU($delid);
			}
		}
		
		if ($this->input->post('result')){
			
		}
		
		$data['list'] = $this->CompanyModel->getSU($id);
		$data['companies'] = $this->CompanyModel->get();
		$data['id'] = $id;
		
		//print_r($data['list']);
		
		$this->load->view('admin/common/header',$this->headerdata);
		$this->load->view('admin/common/left');
		$this->load->view('admin/company_su',$data);
		$this->load->view('admin/common/footer');
	}
	
	function result(){
		$id = $this->input->post('id');
		$result = $this->input->post('result');
		$result+= 1;
		if (is_numeric($id) && $id < 0){
			$id = -$id;
			$this->db->where('id',$id);
			$this->db->update('link_other',array('result'=>$result));
			
		}else if (strstr($id,'|')){
			$tmp = explode('|', $id);
			
			$this->db->where('companyid',$tmp[0]);
			$this->db->where('userid',$tmp[1]);
			$this->db->update('link',array('result'=>$result));
			
		}else{
			die('Error');
		}
		die('Done');
	}
	
	function export($id=0){
		$list = $this->CompanyModel->getSU(0);
		
		$table = array();
		$headrow = array('ID','Name','Company','CV','Result','Mon','Tue','Wed','Thu','Fri');
		$table[] = $headrow;
		$i = 0;
		foreach ($list as $l){
			$i++;
			$freetime = explode('|',$l->freetime);
			$row = array();
			$row[] = $i;
			$row[] = $l->fullname;
			$row[] = $l->name;
			$row[] = $l->cv;
			$row[] = (($l->result==2)?'v':'');
			for ($j=0;$j<5;$j++){
				$row[] = (($freetime[$j*2])?'AM ':'').(($freetime[$j*2+1])?'PM ':'');
			}
			$table[] = $row;
		}
		//print_r($table);
		$this->load->helper('excel');
		to_excel($table);
	}
}
?>